package com.ytjj.qmyx.supplychain.api.ceb.controller;


import com.alibaba.fastjson.JSONObject;
import com.ytjj.qmyx.supplychain.mapper.ReturnGoodsMapper;
import com.ytjj.qmyx.supplychain.mapper.entity.YxReturnGoods;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Map;

/**
 *
 */
@Slf4j
@RestController
@RequestMapping("/link/refund")
public class LINKRefundController {

    @Resource
    private ReturnGoodsMapper returnGoodsMapper;

    /**
     * link 回调接口-退货入库
     * @return
     */
    @PostMapping("/callback/confirm")
    public String receiveRefundConfirmCallback(@RequestParam Map<String, String> request) {
        log.info("===link退货入库信息回传==={}", request);

        JSONObject result = new JSONObject();
        result.put("success", true);
        result.put("errorCode", "200");
        result.put("errorMsg", "成功");

        String refundOrderId = request.get("refundOrderId");
        Example example = new Example(YxReturnGoods.class);
        example.createCriteria().andEqualTo("linkServiceNo",refundOrderId).andIn("status", Arrays.asList(3,8));
        example.setOrderByClause("id desc limit 1");
        YxReturnGoods yxReturnGoods = returnGoodsMapper.selectOneByExample(example);
        if (null != yxReturnGoods){
            int count = returnGoodsMapper.updateBankerStatusByLink(yxReturnGoods.getServiceNo());
            if (count > 0){
                return result.toJSONString();
            }
        }

        result.put("success", false);
        result.put("errorCode", "500");
        result.put("errorMsg", "更新失败");
        return result.toJSONString();
    }
}
